package com.example.question.list.list2;

import com.example.question.list.list1.List;
import com.example.question.list.list1.ListNode;

/**
 * @Date 2023-07-08
 * @Author Jonathan
 */
public class Code07_25 {
    public static void main(String[] args) {
        Code07_25 code = new Code07_25();
        ListNode l1 = List.createList(new int[]{1, 3, 5});
        ListNode l2 = List.createList(new int[]{2, 4, 6});
        System.out.println(code.mergeTwoLists(l1, l2));
    }

    public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
        ListNode dummyHead = new ListNode(-1);
        ListNode retCur = dummyHead;
        ListNode cur1 = list1;
        ListNode cur2 = list2;
        while (cur1 != null && cur2 != null) {
            if (cur1.val < cur2.val) {
                retCur.next = cur1;
                cur1 = cur1.next;
            } else {
                retCur.next = cur2;
                cur2 = cur2.next;
            }
            retCur = retCur.next;
        }
        if (cur1 != null) {
            retCur.next = cur1;
        }
        if (cur2 != null) {
            retCur.next = cur2;
        }
        return dummyHead.next;
    }
}
